import { ref } from "vue";
import { ElNotification } from "element-plus";
import { useCategoryStore } from "@/store/category.ts";

export default function useCategory() {

  const dialog = ref()
  const subDialog = ref()
  const categoryStore = useCategoryStore()

  // 添加分类
  const addCategory = () => {
    dialog.value.init({}, 0)
  }

  // 编辑分类
  const EditCategory = (row: any) => {
    dialog.value.init(row, 1)
  }

  // 删除分类
  const deleteCategory = async () => {
    ElNotification({
      type: 'warning',
      title: '失败',
      message: '一级分类暂不支持删除',
      duration: 2000
    })
  }

  // 添加二级分类
  const addSubCategory = (id: string) => {
    subDialog.value.init(id, {}, 0)
  }

  // 编辑二级分类
  const editSubCategory = (id: string, row: any) => {
    subDialog.value.init(id, row, 1)
  }

  return {
    dialog,
    subDialog,
    editSubCategory,
    categoryStore,
    addCategory,
    EditCategory,
    deleteCategory,
    addSubCategory
  }
}
